//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.ServiceModel.Security
{
    using System;
    using System.Linq;
    using System.Net.Http;
    using Preflight.Security;

    internal class StandardPreflightRequestAuthorization : IPreflightRequestAuthorization
    {
        private readonly ISecureTokenValidator tokenValidator;

        public StandardPreflightRequestAuthorization(ISecureTokenValidator tokenValidator)
        {
            this.tokenValidator = tokenValidator;
        }

        public bool IsAuthorized(HttpRequestMessage request)
        {
            Log.LogVerbose("Authorizing action: {0}", request.RequestUri);
            if (request.Headers.Contains(SecurityConstants.PreflightTokenHeaderName) == false)
            {
                Log.LogVerbose("Request is denied because security header was not included");
                return false; 
            }

            var token = request.Headers.GetValues(SecurityConstants.PreflightTokenHeaderName).Single();

            if (string.IsNullOrWhiteSpace(token))
            {
                Log.LogVerbose("Request is denied because token was empty");
                return false; 
            }

            var result = this.tokenValidator.IsTokenValid(token, request.RequestUri.ToString());
            Log.LogVerbose("Token Validation Result: {0}", result);
            return result;
        }

        public static IPreflightRequestAuthorization Create(PreflightServiceOptions serviceOptions)
        {
            var tokenValidity = TimeSpan.FromSeconds(30);
            var certificateLocation = serviceOptions.CertificateLocation;
            Log.LogVerbose("Building request authorization. Token Validity: {0} Certificate Used: {1}", tokenValidity, certificateLocation);
            return new StandardPreflightRequestAuthorization(SecurityFactory.CreateTokenValidator(certificateLocation, tokenValidity));
        }
    }
}
